//lym 2017.11 新增 月历选择的年份和月份
var dataObj=new Object;//选择的年份和月份
//判断是否是ios 及ios版本
var agent = navigator.userAgent.toLowerCase() ;
var version;
var version_family;
if(agent.indexOf("like mac os x") > 0){
    //ios
    var regStr_saf = /os [\d._]*/gi ;
    var verinfo = agent.match(regStr_saf) ;
    version = (verinfo+"").replace(/[^0-9|_.]/ig,"").replace(/_/ig,"."); 
}
//判断ios版本
var version_str = version+"";
if(version_str != "undefined" && version_str.length >0){
    version=version.substring(0,1);
    if(version>=9){         
		version_family='PingFangSC-Regular';
    }
    else{
    //version_family='STXihei';
    version_family='sans-serif'
    }
}
function calendarMonthdata(year,month,line_num,weekName,width,height,style){//line_num每一行多少天  weekName0为星期，1为 F，2为FRI
	//style={size:50,color:"#000000",align:"center",max:50,line_space:1.3,direction:0};
	/*line_num=14;
	weekName=1;*/
	
	
	if(style.direction == 1 || style.direction == 3) {
		var h=height;
		var w=width;
		width=h;
		height=w;
		
	}
	
	style.direction=0;
	var cur_mon=LunarCalendar.calendar(year,month,false);
	if(Number(line_num)==0){
		line_num=cur_mon.monthDays;
	}
	var elemlist=new Array;
	var x_width=width/line_num;//每列的宽
	var lie_num;
	if(line_num==7){
		lie_num=Math.ceil((Number(cur_mon.monthDays)+Number(cur_mon.firstDay))/line_num)+1//列的数量
	}else{
		lie_num=Math.ceil(Number(cur_mon.monthDays)/line_num)+1//列的数量
	}
	var x_height=height/lie_num//每列的高	
	if(line_num==7){//每行七天时候 星期的数据的组合
		lineW=cur_mon.firstDay;
		//星期的遍历	
		elemlist=elemlist.concat(weekdata(0,7,x_width,x_height,weekName,style,0));
		
	}/*else{	
		var cur_line_num=line_num;
		for(var k=0; k<cur_line_num;k++){
			if(k==0){
				var _x=-cur_mon.firstDay*x_width;
				elemlist=elemlist.concat(weekdata(cur_mon.firstDay,7,x_width,x_height,weekName,style,_x));
				cur_line_num=cur_line_num-(cur_mon.firstDay+1);
			}else{
				var _x=(cur_mon.firstDay+1+(k-1)*7)*x_width;
				if(cur_line_num>7){					
					elemlist=elemlist.concat(weekdata(0,7,x_width,x_height,weekName,style,_x));
					cur_line_num=cur_line_num-7;
				}else{
					elemlist=elemlist.concat(weekdata(0,cur_line_num+1,x_width*k*7,x_height,weekName,style,_x));
					cur_line_num=0;
				}
				
			}
			
		}
	}*/
	var temp_lie_num=1;
	var temp_line_num=0;
	for(var i=0; i<cur_mon.monthData.length; i++){	
		var element_x=0,element_y=0;
		var item=cur_mon.monthData[i];
		if(line_num==7){//每行七天时候 的数据的组合
			if(temp_lie_num==1){//第一列
				temp_line_num=i+cur_mon.firstDay;
				if(temp_line_num>=line_num*temp_lie_num){
					temp_lie_num++;	
					temp_line_num=0;
				}	
				
			}else{				
				temp_line_num=Math.abs(i+cur_mon.firstDay-(temp_lie_num-1)*line_num);
			}			
		}else{
			if(i<line_num){
				var weekcolor=item.weekName=="星期日"||item.weekName=="星期六"?style.color:'#000000';	
				var weeksize=Number(style.size)*0.6;
				var weeksty={size:weeksize,color:weekcolor,align:style.align,max:style.max,line_space:style.line_space,direction:style.direction};		
				var weekcontent=weekstr(item.weekName,weekName);
				var weekday={element_x:x_width*i,element_y:(x_height-weeksize/1.2)/2,content:weekcontent,width:x_width,height:x_height,style:JSON.stringify(weeksty)};
				elemlist.push(weekday);
				temp_line_num=i;
			}else{
				temp_line_num=i%line_num;
			}
			
			
			
					
			
		}
		
						
		element_x=temp_line_num*x_width;
		//element_y=temp_lie_num*x_height-style.size-style.size*style.line_space;
		//element_y=temp_lie_num*x_height-style.size*style.line_space;
		element_y=temp_lie_num*x_height;		
		var lunarcolor, daycolor;		
		var lunarstr, daystr=item.day;
		
		if(item.weekName=="星期日"||item.weekName=="星期六"){//周末
			
			if(item.display==1){//农历节日
				daycolor=style.color;//"#fef668";
				lunarcolor=style.color;//"#fef668";
			}else{//非节日
				lunarcolor=style.color;//"#c1fa28";	
				daycolor=style.color;//"#c1fa28";
			}
			
		}else{
			
			if(item.display==1){//节日
				lunarcolor=style.color;//"#fef668";
				daycolor=style.color;//"#fef668";
			}else{
				lunarcolor="#000000";//"#c1fa28";
				daycolor="#000000";
			}
		}
		
		if(item.lunarFestival==undefined){
			if(item.solarFestival==undefined){
				if(item.term==undefined){
					lunarstr=item.lunarDayName=='初一'?item.lunarMonthName:item.lunarDayName;
					
				}else{
					lunarstr=item.term;
					lunarcolor=style.color;//"#fef668";
				}
				
			}else{
				lunarstr=item.solarFestival;
			}
		}else{
			lunarstr=item.lunarFestival;
		}
		
		
		//lunarstr=item.lunarFestival==undefined?item.lunarDayName:item.lunarFestival;
		//var daysty={size:50,color:daycolor,align:"center",max:50,line_space:1,direction:0};
		//var lunarsty={size:50,color:lunarcolor,align:"center",max:50,line_space:1,direction:0};
		var daysty={size:style.size,color:daycolor,align:style.align,max:style.max,line_space:style.line_space,direction:style.direction};
		var lunarsty={size:Number(style.size)*0.5,color:lunarcolor,align:style.align,max:style.max,line_space:style.line_space,direction:style.direction};
		
		
		
		var dayobj={element_x:element_x,element_y:element_y,content:daystr.toString(),width:x_width,height:x_height,style:JSON.stringify(daysty),id:''};
			elemlist.push(dayobj);		
		var lunarDay={element_x:element_x,element_y:element_y+Number(style.size/1.2)*(Number(style.line_space)+1),content:lunarstr,width:x_width,height:x_height,style:JSON.stringify(lunarsty)};
		elemlist.push(lunarDay);
	if(line_num==7){
		if((i+cur_mon.firstDay+1)>=line_num*temp_lie_num){
			temp_lie_num++;	
			temp_line_num=0;
		}	
	}else{
		if((i+1)%line_num==0){
			temp_lie_num++;	
			temp_line_num=0;
		} 
	}
		
	
	}
	return 	elemlist;
	
	
}

//把星期的数据存到临时的数组中
function weekdata(k,weeklen,width,height,weekName,style,_x){
	var weekelem=new Array;
	for(var i=k; i<weeklen; i++){//星期的遍历	
		var colorsty=i==0||i==6?style.color:'#000000';	
		var weeksize=Number(style.size)*0.6;
		var weeksty={size:weeksize,color:colorsty,align:style.align,max:style.max,line_space:style.line_space,direction:style.direction};		
		var weekcontent=weekstr(i,weekName);			
		var lunarDay={element_x:Number(width*i+_x),element_y:(height-weeksize/1.2)/2,content:weekcontent,width:width,height:height,style:JSON.stringify(weeksty)};
		weekelem.push(lunarDay);
	}	
	
	return 	weekelem;
}


//得到星期的字符串
function weekstr(i,weekName){
	var str='';
	if(i==0||i=='星期日'){
		str=weekName!=1?(weekName==0?'星期日':'Sunday'):'SUN';
	}else if(i==1||i=='星期一'){
		str=weekName!=1?(weekName==0?'星期一':'Monday'):'MON';
	}else if(i==2||i=='星期二'){
		str=weekName!=1?(weekName==0?'星期二':'Tuesday'):'TUE';
	}else if(i==3||i=='星期三'){
		str=weekName!=1?(weekName==0?'星期三':'Wednesday'):'WED';
	}else if(i==4||i=='星期四'){
		str=weekName!=1?(weekName==0?'星期四':'Thursday'):'THU';
	}else if(i==5||i=='星期五'){
		str=weekName!=1?(weekName==0?'星期五':'Friday'):'FRI';
	}else if(i==6||i=='星期六'){
		str=weekName!=1?(weekName==0?'星期六':'Saturday'):'SAT';
	}
	return str;
}


//日历单独生成新的canvas
function calendarTocanvas(ctxt,element,dataList,scale){

	var canvas=document.createElement('canvas');
	var context = canvas.getContext('2d');
	var elesty=JSON.parse(element.element_list[0].style);	
	if(elesty.direction == 1 || elesty.direction == 3) {
		canvas.width=element.height*scale;
		canvas.height=element.width*scale;
		
	}else{
		canvas.width=element.width*scale;
		canvas.height=element.height*scale;
	}
	for(var d = 0; d < dataList.length; d++) {
		var list = dataList[d];
		if(list.style != "" && list.style != undefined) {
			var sty = JSON.parse(list.style);			
			context.beginPath();
			
			var TextObj = new Object;
			var fontsize = parseInt(Number(sty.size) * scale);
			if(fontsize < 12) {
				var newscale = 12 / fontsize;
				TextObj = {
					x: parseInt(list.element_x * scale * newscale + element.coordinate_x * scale * newscale),
					y: parseInt(list.element_y * scale * newscale + element.coordinate_y * scale * newscale),
					width: parseInt(list.width * scale * newscale),
					height: parseInt(list.height * scale * newscale),
					family: version!="undefined"?version_family:'Microsoft YaHei',
					size: 12 + "px",
					weight: '',
					color: sty.color.toString(),
					style: '',
					center: sty.align != '' ? sty.align.toString() : "center",
					lineHeight: parseInt((Number(sty.size) * Number(sty.line_space)) * scale * newscale),
					text: list.content,
					transform: sty.direction * 90,
					newscale: newscale,
					ischange: false
				}
	
			} else {
				TextObj = {
					x: parseInt(list.element_x * scale + element.coordinate_x * scale),
					y: parseInt(list.element_y * scale + element.coordinate_y * scale),
					width: parseInt(list.width * scale),
					height: parseInt(list.height * scale),
					family: version!="undefined"?version_family:'Microsoft YaHei',
					size: parseInt(Number(sty.size) * scale) + "px",
					weight: '',
					color: sty.color.toString(),
					style: '',
					center: sty.align != '' ? sty.align.toString() : "center",
					lineHeight: parseInt((Number(sty.size) * Number(sty.line_space)) * scale),
					text: list.content,
					transform: sty.direction * 90,
					newscale: 1,
					ischange: false
				}
			}
	
			if(TextObj.transform > 0) {
				if(TextObj.transform == 90 || TextObj.transform == 270) {
					var w = TextObj.height;
					var h = TextObj.width;
					TextObj.width = w;
					TextObj.height = h;
				}
			}
			drawlinetext(TextObj, function(result, strenter, data) {
				context.drawImage(data, list.element_x * scale , list.element_y * scale, list.width * scale, list.height * scale);
				context.rect(list.element_x * scale, list.element_y * scale, list.width * scale, list.height * scale);
				context.closePath();
				context.save();					
				context.restore();
	
			});
		}
	}
	if(elesty.direction == 1 || elesty.direction == 3) {
		textRoate(canvas,canvas.width,canvas.height,ctxt,elesty.direction*90,element.coordinate_x*scale,element.coordinate_y*scale,1);
	}else{
		ctxt.drawImage(canvas,element.coordinate_x*scale,element.coordinate_y*scale,element.width*scale, element.height*scale);
							
	}
						
	ctxt.rect(element.coordinate_x*scale, element.coordinate_y*scale, element.width*scale, element.height*scale);
	ctxt.closePath();
	ctxt.save();
	ctxt.clip();	
	ctxt.restore();	
}

//把修改日期后的月历重新绘制到canvas
 function comfrimDateToCanvas(){//lym 2017 11.20新增月历修改	
 		var k=$(".comfirm_date").attr("currentid"); 		
		var index= $("#originalcanvas").attr("page");
		var canvasData=UtilProperty[work_id].worker[index].control_list;		
		for(var i=0; i<canvasData.length;i++){			
				if(k==canvasData[i].id){
					var element=canvasData[i];
					if(element.element_list.length>0){
						var dateList=element.element_list[0];								
						if(dateList.style!=""&&dateList.style!=undefined){
							var a=JSON.parse(dateList.style);
							var fontstyle={align:a.align,color:a.color,direction:a.direction,line_space:a.line_space,max:a.max,size:a.size,format:a.format};
							UtilProperty[work_id].worker[index].control_list[i].element_list[0].content=dataObj.year+'-'+dataObj.month;
							UtilProperty[work_id].worker[index].control_list[i].element_list[0].date_list=calendarMonthdata(dataObj.year,dataObj.month,a.column,a.weekstyle,element.width,element.height,fontstyle);
							UtilProperty[work_id].worker[index].control_list[i].ischange=true;
						}
					}
				}
			if(canvasData[i].control_id==2&&canvasData[i].element_list[0].content!=""&&canvasData[i].disabled==1){
				a=JSON.parse(canvasData[i].element_list[0].style);
				if(a.format!=""){			
					if(a.format=="YYYY"){
						UtilProperty[work_id].worker[index].control_list[i].element_list[0].content=dataObj.year;
					}else if(a.format=="YY"){
						UtilProperty[work_id].worker[index].control_list[i].element_list[0].content=dataObj.year.substring(-1,2)+'年';
					}else if(a.format=="MM"){
						UtilProperty[work_id].worker[index].control_list[i].element_list[0].content=dataObj.month;
					}
					UtilProperty[work_id].worker[index].control_list[i].ischange=true;
				}
			}
		
		}
		$(".comfirm_date").removeAttr("currentid");
		ctxdate ={};	
		changeoriginalcanvas(index);		
		changepagemainbox(index);
 }

